#include "UniformGridTest.h"

CPPUNIT_TEST_SUITE_REGISTRATION(UniformGridTest);

using physicsworks::vec3;
using physicsworks::instanceUniverse;
using physicsworks::Universe;
using std::list;
using std::pair;

void UniformGridTest::setUp()
{
	Universe* universe = instanceUniverse();

	BodyMesh *monkeyMesh, *chairMesh;	
	monkeyMesh = new BodyMesh("tests/data/monkey.mesh.xml");
	chairMesh = new BodyMesh("tests/data/chair.mesh.xml");
	
	monkey = dynamic_cast<RigidBody*>(universe->instanceBody(monkeyMesh->vertX, monkeyMesh->vertY, monkeyMesh->vertZ, monkeyMesh->numVertices,
	                                monkeyMesh->verticesIndices, monkeyMesh->numFaces));
	delete monkeyMesh;
	
	chair = dynamic_cast<RigidBody*>(universe->instanceBody(chairMesh->vertX, chairMesh->vertY, chairMesh->vertZ, chairMesh->numVertices,
	                               chairMesh->verticesIndices, chairMesh->numFaces));
	delete chairMesh;
	
	sphere = dynamic_cast<RigidBody*>(universe->instanceSphereBody(0.3));
	cube = dynamic_cast<RigidBody*>(universe->instanceCubeBody(1));
	box = dynamic_cast<RigidBody*>(universe->instanceBoxBody(1, 1, 1));
	
	uniformGrid = new UniformGrid(vec3(-300, -300, -300), vec3(800, 700, 700), 2, 100);
}

void UniformGridTest::tearDown()
{
	
}

void UniformGridTest::testAddingRemovingBodies()
{
	uniformGrid->update();
	
	chair->setPosition(0, 0, 0);
	uniformGrid->add(chair);
	uniformGrid->update();
	
	chair->setPosition(50, 0, 0);
	uniformGrid->update();
	
	box->setPosition(0, 0, 0);
	uniformGrid->add(box);
	uniformGrid->update();
	
	monkey->setPosition(10, 20, 0);
	uniformGrid->add(monkey);
	uniformGrid->update();
	
	sphere->setPosition(1200, 1500, 0);
	uniformGrid->add(sphere);
	uniformGrid->update();
	
	sphere->setPosition(-10, 10, 5);
	uniformGrid->update();
}

void UniformGridTest::testRetrievingBodyPairs()
{
	list<pair<RigidBody*,RigidBody*> > pairs = uniformGrid->getPotentialCollidingPairs();
	CPPUNIT_ASSERT(pairs.empty() == true);
	uniformGrid->add(chair);
	pairs = uniformGrid->getPotentialCollidingPairs();
	CPPUNIT_ASSERT(pairs.size() == 0);
	uniformGrid->add(box);
	pairs = uniformGrid->getPotentialCollidingPairs();
	CPPUNIT_ASSERT(pairs.size() == 1);
	uniformGrid->add(monkey);
	pairs = uniformGrid->getPotentialCollidingPairs();
	CPPUNIT_ASSERT(pairs.size() == 3);
	uniformGrid->add(sphere);
	pairs = uniformGrid->getPotentialCollidingPairs();
	CPPUNIT_ASSERT(pairs.size() == 6);
}
